package com.Xingcang_Dianxue.WhutBank.Controllers.Client;

import com.Xingcang_Dianxue.WhutBank.Models.Model;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

public class DashboardController implements Initializable {

    @FXML
    private AnchorPane checking_pane;

    @FXML
    private Label checking_acc_num;

    @FXML
    private Label checking_balance;

    @FXML
    private Label expense_lbl;

    @FXML
    private Label income_lbl;

    @FXML
    private Text user_name;

    @FXML
    private Text time_lbl;

    @FXML
    private AnchorPane dashboard_container;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        updateTime();
        bindData();
    }



    // 将用户信息绑定到界面上显示
    private void bindData() {
        user_name.textProperty().bind(Bindings.concat("Welcome, ").concat(Model.getInstance().getClient().userNameProperty()));
        // 判断用户是否有银行卡
        if (Model.getInstance().getClient().getCheckingAccount() != null) {
            String[] checking_num = Model.getInstance().getClient().getCheckingAccount().getAccountNumber().split(" ");
            checking_acc_num.textProperty().bind(Bindings.concat(checking_num[0], " ", "****", " ", checking_num[2], " ", "****"));
            checking_balance.textProperty().bind(Bindings.concat("￥").concat(Model.getInstance().getClient().getCheckingAccount().balanceProperty()));
        } else {
            checking_pane.setVisible(false);
        }
        income_lbl.textProperty().bind(Bindings.concat("￥").concat(Model.getInstance().totalIncomeProperty()));
        expense_lbl.textProperty().bind(Bindings.concat("￥").concat(Model.getInstance().totalExpensesProperty()));
    }


    private void updateTime() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String timeText = formatter.format(date);
        Platform.runLater(() -> time_lbl.setText(timeText));
    }
}
